package com.globallogic.javatrainings.sergiiivanov.controller;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

import com.globallogic.javatrainings.sergiiivanov.view.GUI;

/**
 * Keeps readers of user information
 * 
 * @author Sergey
 * 
 */
class InputController {
    private static Scanner in;
    private static PrintWriter macro;
    private static boolean macroEnabled;
    static boolean useBrute = false;

    /**
     * Reads integer number and still asking until number is entred
     * 
     * @return integer number
     */
    static int readInt() {
        boolean failed = true;
        int filteredInput = 0;
        while (failed) {
            try {
                filteredInput = Integer.parseInt(readString());
                failed = false;
            } catch (Exception e) {
                GUI.getGUI().showValidationError("One number expected. Try again");
            }
        }
        return filteredInput;
    }

    /**
     * Reads text input and still asking until non-empty string entered
     * 
     * @return not empty string
     */
    static String readString() {
        String res = "";
        while (res.equals("")) {
            if (useBrute) {
                res = Brute.nextLine();
                GUI.getGUI().show(res);
            } else {
                res = getScanner().nextLine();
            }
        }
        saveMacro(res);
        return res;
    }

    static void close() {
        try {
            in.close();
        } catch (Exception e) {
            // do nothing - probably closed already
        }
    }

    static Scanner getScanner() {
        if (in == null) {
            in = new Scanner(System.in);
        }
        return in;
    }

    static void saveMacro(String input) {
        if (macroEnabled && macro != null) {
            macro.println(input);
            macro.flush();
        }
    }

    /**
     * Intended to be used by automated testing</br> Replaces input from
     * keyboard with input from plain text file with predefined list of user
     * commands
     * 
     * @param filename
     * @throws FileNotFoundException
     */
    static void setInputFile(String filename) throws FileNotFoundException {
        in = new Scanner(new BufferedReader(new FileReader(filename)));
    }

    static void saveMacroEnabled(String file) throws IOException {
        try {
            macro = new PrintWriter(file);
            macroEnabled = true;
        } catch (FileNotFoundException e) {
            java.io.File f = new java.io.File(file);
            f.createNewFile();
            macro = new PrintWriter(f);
            macroEnabled = true;
        }
    }

    static void closeMacro() {
        if (macroEnabled) {
            macro.close();
        }
    }
}
